package cn.jiyun.interceptor;

import cn.jiyun.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/*
* 自定义登录拦截器
* */
@Slf4j
public class LoginInterCeptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //System.out.println("LoginInterceptor的preHandle执行了");
        log.debug("LoginInterceptor的preHandle执行了");
        //登录状态校验
       User user=(User) request.getSession().getAttribute("user");
       if (user!=null){
           //登录状态，直接放行
           return true;
       }
       //未登录状态，拦截请求
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        //System.out.println("LoginInterceptor的preHandle执行了");
        log.debug("LoginInterceptor的preHandle执行了");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
       // System.out.println("LoginInterceptor的preHandle执行了");
        log.debug("LoginInterceptor的preHandle执行了");
    }
}
